import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class EasyStock extends HttpServlet
{
	public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
	{
		doPost(request,response);
	}//doGet

	public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
	{
		RequestDispatcher dispatcher = null;
		String event = request.getParameter("eventAction");
		if( event.equals("exit") )
		{
			dispatcher = request.getRequestDispatcher("/jsp/OperatorActivities.jsp");
			dispatcher.forward(request,response);
		}
		try
		{
			int amount = Integer.parseInt(request.getParameter("amount"));
			System.out.println("amount"+amount);
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/airtel","root","root");
			Statement stmt = con.createStatement();
			String tid = request.getParameter("tid");
						System.out.println("tid:"+tid);
			String date1 = request.getParameter("collectdate");
			String d = date1.substring(0,2);
			String m = date1.substring(3,5);
			String y = date1.substring(6,10); 
			String ts = y + "-" + m + "-" + d;
						System.out.println("Time::"+ts);
			String ddno = request.getParameter("ddno");
						System.out.println("ddno:::"+ddno);
			System.out.println("Time:"+ts);
			ResultSet rs = stmt.executeQuery("select amount from easystock");
			int easyamount = 0;
			while(rs.next())
			{
				easyamount = Integer.parseInt(rs.getString(1));
			}
				easyamount += amount;
				int id = 0;
				rs = stmt.executeQuery("select max(id) from easystock");
				if(rs.next())
				{
					if( rs.getString(1) != null )
					{
						id = Integer.parseInt(rs.getString(1));
					}
				}
				id++;
				String qry = "insert into easystock values('"+id+"','"+ts+"','"+easyamount+"','"+tid+"','"+ddno+"','"+amount+"')";
				System.out.println("*******"+qry);
				System.out.println("easyamount"+amount);
				stmt.executeUpdate(qry);
				System.out.println("Amount From Else : "+amount);
				request.setAttribute("msg", new String("Easy Recharge amount new entry successfyully"));    
			dispatcher = request.getRequestDispatcher("/jsp/EasyStock.jsp");
			dispatcher.forward(request,response);
		}//try
		catch(Exception e)
		{
			e.printStackTrace();
		}//catch
	}//doPost
}//class